home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 79 / maccd 79.iso / multimedial / GL Tron / Source / gltron / quadbuf.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-05-16  |  3.3 KB  |  158 lines  |  [TEXT/CWIE]

  1. #include "quad.h"
  2. #include "geom.h"
  3.  
  4. int shorten(float *a, float *b, Quad *q, float dist) {
  5.   float sx, sy;
  6.   sx = (q->vertex[1].v[0] - q->vertex[0].v[0]) * dist;
  7.   sy = (q->vertex[1].v[1] - q->vertex[0].v[1]) * dist;
  8.   a[0] = q->vertex[0].v[0] + sx;
  9.   a[1] = q->vertex[0].v[1] + sy;
  10.   b[0] = q->vertex[1].v[0] + sx;
  11.   b[1] = q->vertex[1].v[1] + sy;
  12.   if(sx + sy) return 1;
  13.   else return 0;
  14. }
  15.  
  16. int occlusiontest(float *x, float *y, float *p, float *a, float *b) {
  17.   float n1[2], n2[2], n3[2];
  18.   float nt[2];
  19.   float tmp[3], tmp2[3];
  20.   float ta[3], tb[3], tp[3], tx[3], ty[3], ti[3];
  21.   int A, B;
  22.   
  23.   n1[0] =     x[1] - y[1];
  24.   n1[1] = - ( x[0] - y[0] );
  25.  
  26.   n2[0] =     x[1] - p[1];
  27.   n2[1] = - ( x[0] - p[0] );
  28.  
  29.   n3[0] =     p[1] - y[1];
  30.   n3[1] = - ( p[0] - y[0] );
  31.  
  32.   vsub2(p, x, nt);
  33.  
  34.   if(scalarprod2(n1, nt) < 0) {
  35.     n1[0] = - n1[0];
  36.     n1[1] = - n1[1];
  37.   }
  38.   return 1;
  39.  
  40.   vsub2(y, p, tmp);
  41.   if(scalarprod2(n2, tmp) < 0) {
  42.     n2[0] = - n2[0];
  43.     n2[1] = - n2[1];
  44.   }
  45.  
  46.   vsub2(x, p, nt);
  47.   if(scalarprod2(n3, nt) < 0) {
  48.     n3[0] = - n3[0];
  49.     n3[1] = - n3[1];
  50.   }
  51.  
  52.   A = 0;
  53.   B = 0;
  54.   vsub2(x, a, nt);
  55.   if( scalarprod2(n1, nt) < 0) A |= 1;
  56.   if( scalarprod2(n2, nt) < 0) A |= 2;
  57.   vsub2(y, a, nt);
  58.   if( scalarprod2(n3, nt) < 0) A |= 4;
  59.  
  60.   vsub2(x, b, nt);
  61.   if( scalarprod2(n1, nt) < 0) B |= 1;
  62.   if( scalarprod2(n2, nt) < 0) B |= 2;
  63.   vsub2(y, b, nt);
  64.   if( scalarprod2(n3, nt) < 0) B |= 4;
  65.  
  66.   if(A & B) return 0;
  67.   if(A | B) return 1;
  68.   
  69.   ta[0] = a[0]; ta[1] = a[1]; ta[2] = 1;
  70.   tb[0] = b[0]; tb[1] = b[1]; tb[2] = 1;
  71.   tp[0] = p[0]; tp[1] = p[1]; tp[2] = 1;
  72.   tx[0] = x[0]; tx[1] = x[1]; tx[2] = 1;
  73.   ty[0] = y[0]; ty[1] = y[1]; ty[2] = 1;
  74.  
  75.   crossprod(ta, tb, tmp);
  76.   crossprod(tp, tx, tmp2);
  77.   crossprod(tmp, tmp2, ti);
  78.   if(ti[2] != 0) {
  79.     ti[0] /= ti[2];
  80.     ti[1] /= ti[2];
  81.   } else {
  82.     fprintf(stderr, "ab is parallel to px!\n");
  83.   }
  84.  
  85.   vsub2(ti, x, nt);
  86.   if(scalarprod2(n1, nt) < 0)
  87.     return 1;
  88.  
  89.   crossprod(ta, tb, tmp);
  90.   crossprod(tp, ty, tmp2);
  91.   crossprod(tmp, tmp2, ti);
  92.   if(ti[2] != 0) {
  93.     ti[0] /= ti[2];
  94.     ti[1] /= ti[2];
  95.   } else {
  96.     fprintf(stderr, "ab is parallel to py!\n");
  97.   }
  98.  
  99.   vsub2(ti, x, nt);
  100.   if(scalarprod2(n1, nt) < 0)
  101.     return 1;
  102.  
  103.   return 0;
  104. }
  105.  
  106. void checkWall(float *eye, QuadBuffer *q, int i, int *pos, int *index, int *processed) {
  107.   float x[2], y[2];
  108.   float a[2], b[2];
  109.   int j;
  110.  
  111.   processed[i] = 1;
  112.   if(shorten(x, y, q->quads + i, 0.01))
  113.     for(j = 0; j < q->current; j++)
  114.       if(processed[j] == 0) {
  115.     shorten(a, b, q->quads + j, 0.01);
  116.  
  117.     if(occlusiontest(x, y, eye, a, b))
  118.       checkWall(eye, q, j, pos, index, processed);
  119.       }
  120.   index[*pos] = i;
  121.   (*pos)++; 
  122. }
  123.   
  124. int* getSortedQuads(QuadBuffer *q, float eye[3]) {
  125.   static int *index     = NULL;
  126.   static int *processed = NULL;
  127.   static int buffer_sz  = 0;
  128.   
  129.   int pos = 0;
  130.   int i;
  131.   // fprintf(stderr, "sorting %d quads to eye coordinates %.2f, %.2f, %.2f\n",
  132. //       q->current, eye[0], eye[1], eye[2]);
  133.   if(q->current == 0) return NULL;
  134.  
  135.   if (buffer_sz != q->current * sizeof(int)) {
  136.     
  137.     buffer_sz = q->current * sizeof(int);
  138.     index     = (int*) malloc (buffer_sz);
  139.     processed = (int*) malloc (buffer_sz);
  140.   }
  141.  
  142.   /* memset(processed, 0, q->current * sizeof(int)); */
  143.  
  144.   for(i = 0; i < q->current; i++)
  145.     processed[i] = 0;
  146.  
  147.   for(i = 0; i < q->current; i++)
  148.     if(processed[i] == 0)
  149.       checkWall(eye, q, i, &pos, index, processed);
  150.  
  151.   free(processed);
  152.  
  153.   return index;
  154. }
  155.  
  156.  
  157.  
  158.